package com.hgc.ch02.forkjoin;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.RecursiveAction;

/**
 * Created by 01367627 on 2019/7/8.
 */
public class FJListFiles {

    private static class SubTask extends RecursiveAction {

        private File path;

        private SubTask(File path) {
            this.path = path;
        }

        @Override
        protected void compute() {
            List<SubTask> actions = new ArrayList<>();
            File[] files = path.listFiles();
            if (files != null) {
                for (File file : files) {
                    if (file.isDirectory()) {
                        actions.add(new SubTask(file));
                    } else {
                        System.out.println("文件：" + path.getAbsolutePath());
                    }
                }
                if (!actions.isEmpty()) {
                    for (SubTask task : invokeAll(actions)) {
                        task.join();
                    }
                }
            }
        }
    }

    public static void main(String[] args) throws InterruptedException, IOException {
        ForkJoinPool pool = new ForkJoinPool();
        SubTask subTask = new SubTask(new File("F:/"));
        pool.execute(subTask);
        Thread.sleep(1000);
    }

}
